package top.kaoshanji.p1ch02c;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
 * author kaoshanji
 * ^_^
 * create time 2025/1/2 19:21
 */
public class MethodHandleExample03 {

    private static final Logger logger = LogManager.getLogger(MethodHandleExample03.class);

    public static void main(String[] args) throws Throwable {

        MethodHandleExample03 example = new MethodHandleExample03();
        example.asSpreader();

    }

    public void asSpreader() throws Throwable {
        logger.info("...代码清单 p1ch02c-09：MethodHandle#asSpreader的使用示例...");

        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle mh = lookup.findVirtual(MethodHandleExample03.class, "toBeSpreaded",
                MethodType.methodType(void.class, String.class, int.class, int.class, int.class));

        mh = mh.asSpreader(int[].class, 3);
        mh.invoke(this, "Hello", new int[]{3, 4, 5});

        System.out.println(mh.type());

    }


    public void toBeSpreaded(String arg1, int arg2, int arg3, int arg4) {}

}
